home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / perl5 / Debconf / DbDriver / Stack.pm < prev   
Encoding:
Perl POD Document  |  2009-03-24  |  5.0 KB  |  241 lines

  1. #!/usr/bin/perl -w
  2. # This file was preprocessed, do not edit!
  3.  
  4.  
  5. package Debconf::DbDriver::Stack;
  6. use strict;
  7. use Debconf::Log qw{:all};
  8. use Debconf::Iterator;
  9. use base 'Debconf::DbDriver::Copy';
  10.  
  11.  
  12.  
  13. use fields qw(stack);
  14.  
  15.  
  16. sub init {
  17.     my $this=shift;
  18.  
  19.     if (! ref $this->{stack}) {
  20.         my @stack;
  21.         foreach my $name (split(/\s*,\s/, $this->{stack})) {
  22.             my $driver=$this->driver($name);
  23.             unless (defined $driver) {
  24.                 $this->error("could not find a db named \"$name\" to use in the stack (it should be defined before the stack in the config file)");
  25.                 next;
  26.             }
  27.             push @stack, $driver;
  28.         }
  29.         $this->{stack}=[@stack];
  30.     }
  31.  
  32.     $this->error("no stack set") if ! ref $this->{stack};
  33.     $this->error("stack is empty") if ! @{$this->{stack}};
  34. }
  35.  
  36.  
  37. sub iterator {
  38.     my $this=shift;
  39.  
  40.     my %seen;
  41.     my @iterators = map { $_->iterator } @{$this->{stack}};
  42.     my $i = pop @iterators;
  43.     my $iterator=Debconf::Iterator->new(callback => sub {
  44.         for (;;) {
  45.             while (my $ret = $i->iterate) {
  46.                 next if $seen{$ret};
  47.                 $seen{$ret}=1;
  48.                 return $ret;
  49.             }
  50.             $i = pop @iterators;
  51.             return undef unless defined $i;
  52.         }
  53.     });
  54. }
  55.  
  56.  
  57. sub shutdown {
  58.     my $this=shift;
  59.  
  60.     my $ret=1;
  61.     foreach my $driver (@{$this->{stack}}) {
  62.         $ret=undef if not defined $driver->shutdown(@_);
  63.     }
  64.     return $ret;
  65. }
  66.  
  67.  
  68. sub exists {
  69.     my $this=shift;
  70.  
  71.     foreach my $driver (@{$this->{stack}}) {
  72.         return 1 if $driver->exists(@_);
  73.     }
  74.     return 0;
  75. }
  76.  
  77. sub _query {
  78.     my $this=shift;
  79.     my $command=shift;
  80.     shift; # this again
  81.     
  82.     debug "db $this->{name}" => "trying to $command(@_) ..";
  83.     foreach my $driver (@{$this->{stack}}) {
  84.         if (wantarray) {
  85.             my @ret=$driver->$command(@_);
  86.             debug "db $this->{name}" => "$command done by $driver->{name}" if @ret;
  87.             return @ret if @ret;
  88.         }
  89.         else {
  90.             my $ret=$driver->$command(@_);
  91.             debug "db $this->{name}" => "$command done by $driver->{name}" if defined $ret;
  92.             return $ret if defined $ret;
  93.         }
  94.     }
  95.     return; # failure
  96. }
  97.  
  98. sub _change {
  99.     my $this=shift;
  100.     my $command=shift;
  101.     shift; # this again
  102.     my $item=shift;
  103.  
  104.     debug "db $this->{name}" => "trying to $command($item @_) ..";
  105.  
  106.     foreach my $driver (@{$this->{stack}}) {
  107.         if ($driver->exists($item)) {
  108.             last if $driver->{readonly}; # nope, hit a readonly one
  109.             debug "db $this->{name}" => "passing to $driver->{name} ..";
  110.             return $driver->$command($item, @_);
  111.         }
  112.     }
  113.  
  114.     my $src=0;
  115.  
  116.     foreach my $driver (@{$this->{stack}}) {
  117.         if ($driver->exists($item)) {
  118.             my $ret=$this->_nochange($driver, $command, $item, @_);
  119.             if (defined $ret) {
  120.                 debug "db $this->{name}" => "skipped $command($item) as it would have no effect";
  121.                 return $ret;
  122.             }
  123.  
  124.             $src=$driver;
  125.             last
  126.         }
  127.     }
  128.  
  129.     my $writer;
  130.     foreach my $driver (@{$this->{stack}}) {
  131.         if ($driver == $src) {
  132.             debug "db $this->{name}" =>
  133.                 "$src->{name} is readonly, and nothing above it in the stack will accept $item -- FAILURE";
  134.             return;
  135.         }
  136.         if (! $driver->{readonly}) {
  137.             if ($command eq 'addowner') {
  138.                 if ($driver->accept($item, $_[1])) {
  139.                     $writer=$driver;
  140.                     last;
  141.                 }
  142.             }
  143.             elsif ($driver->accept($item)) {
  144.                 $writer=$driver;
  145.                 last;
  146.             }
  147.         }
  148.     }
  149.     
  150.     unless ($writer) {
  151.         debug "db $this->{name}" => "FAILED $command";
  152.         return;
  153.     }
  154.  
  155.     if ($src) {        
  156.         $this->copy($item, $src, $writer);
  157.     }
  158.  
  159.     debug "db $this->{name}" => "passing to $writer->{name} ..";
  160.     return $writer->$command($item, @_);
  161. }
  162.  
  163. sub _nochange {
  164.     my $this=shift;
  165.     my $driver=shift;
  166.     my $command=shift;
  167.     my $item=shift;
  168.  
  169.     if ($command eq 'addowner') {
  170.         my $value=shift;
  171.         foreach my $owner ($driver->owners($item)) {
  172.             return $value if $owner eq $value;
  173.         }
  174.         return;
  175.     }
  176.     elsif ($command eq 'removeowner') {
  177.         my $value=shift;
  178.         
  179.         foreach my $owner ($driver->owners($item)) {
  180.             return if $owner eq $value;
  181.         }
  182.         return $value; # no change
  183.     }
  184.     elsif ($command eq 'removefield') {
  185.         my $value=shift;
  186.         
  187.         foreach my $field ($driver->fields($item)) {
  188.             return if $field eq $value;
  189.         }
  190.         return $value; # no change
  191.     }
  192.  
  193.     my @list;
  194.     my $get;
  195.     if ($command eq 'setfield') {
  196.         @list=$driver->fields($item);
  197.         $get='getfield';
  198.     }
  199.     elsif ($command eq 'setflag') {
  200.         @list=$driver->flags($item);
  201.         $get='getflag';
  202.     }
  203.     elsif ($command eq 'setvariable') {
  204.         @list=$driver->variables($item);
  205.         $get='getvariable';
  206.     }
  207.     else {
  208.         $this->error("internal error; bad command: $command");
  209.     }
  210.  
  211.     my $thing=shift;
  212.     my $value=shift;
  213.     my $currentvalue=$driver->$get($item, $thing);
  214.     
  215.     my $exists=0;
  216.     foreach my $i (@list) {
  217.         $exists=1, last if $thing eq $i;
  218.     }
  219.     return $currentvalue unless $exists;
  220.  
  221.     return $currentvalue if $currentvalue eq $value;
  222.     return undef;
  223. }
  224.  
  225. sub addowner    { $_[0]->_change('addowner', @_)    }
  226. sub removeowner { $_[0]->_change('removeowner', @_)    }
  227. sub owners    { $_[0]->_query('owners', @_)        }
  228. sub getfield    { $_[0]->_query('getfield', @_)        }
  229. sub setfield    { $_[0]->_change('setfield', @_)    }
  230. sub removefield { $_[0]->_change('removefield', @_)    }
  231. sub fields    { $_[0]->_query('fields', @_)        }
  232. sub getflag    { $_[0]->_query('getflag', @_)        }
  233. sub setflag    { $_[0]->_change('setflag', @_)        }
  234. sub flags    { $_[0]->_query('flags', @_)        }
  235. sub getvariable { $_[0]->_query('getvariable', @_)    }
  236. sub setvariable { $_[0]->_change('setvariable', @_)    }
  237. sub variables    { $_[0]->_query('variables', @_)    }
  238.  
  239.  
  240. 1
  241.